const Controller = require('../../core/controller/ApiController');
const {Route, HttpGet,HttpPost, Middleware, filters} = require('../../../plugin/egg-decorator-router/lib');
const {ExceptionHandler} = require('../../../plugin/egg-controller-exception-handler/lib');

@Route('/api/miniapp/record')
class RecordCtrl extends Controller {

    //借用记录排行
    @HttpGet('/runk')
    @ExceptionHandler()
    async getRecordRunk() {
        this.result.data = await this.service.miniapp.record.main.getRecordRunk();
    }

    //借用记录个人排名
    @HttpGet('/personal/runk')
    @ExceptionHandler()
    async personal() {
        const rule = {
            open_id:'string'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.record.main.personal(this.ctx.request.query.open_id);
    }

    //借用记录
    @HttpGet('/:user_id')
    @ExceptionHandler()
    async getRecord() {
        const rule = {
            user_id: 'int'
        };
        this.validate(rule,this.ctx.params);
        this.result.data = await this.service.miniapp.record.main.getRecord(this.ctx.params.user_id);
    }

    //设备借出
    @HttpPost('/add')
    @ExceptionHandler()
    async addRecord() {
        const rule = {
            device_id: 'int',
            user_id: 'int'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.record.main.addRecord(this.ctx.request.query);
        return '创建成功'
    }

    //提交
    @HttpPost('/commit')
    @ExceptionHandler()
    async commit() {
        const rule = {
            device_id: 'int',
            user_id: 'int',
            status: 'int'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.record.main.commit(this.ctx.request.query);
    }

    //延迟
    @HttpPost('/update/delay')
    @ExceptionHandler()
    async delay() {
        const rule = {
            record_id: 'int',
            device_id: 'int',
            user_id: 'int'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.record.main.delay(this.ctx.request.query);
    }
}

module.exports = RecordCtrl;
